package com.zx.JDK特性.jdk_11;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.apache.tools.ant.util.DateUtils;
import org.junit.Test;

public class JDK11_demo {

    @Test
    public void test1() {
        String str = "woshidage";
        boolean isblank = str.isBlank();  //判断字符串是空白
        boolean isempty = str.isEmpty();  //判断字符串是否为空
        String result1 = str.strip();    //首位空白
        String result2 = str.stripTrailing();  //去除尾部空白
        String result3 = str.stripLeading();  //去除首部空白
        String copyStr = str.repeat(2);  //复制几遍字符串
        long lineCount = str.lines().count();  //行数统计

        System.out.println(isblank);
        System.out.println(isempty);
        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(copyStr);
        System.out.println(lineCount);
    }

    //同步调用
    @Test
    public void test2() throws Exception {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create("http://www.zx1989.top/")).build();
        BodyHandler<String> handler = HttpResponse.BodyHandlers.ofString();
        HttpResponse<String> response = client.send(request, handler);
        String body = response.body();
        System.out.println(body);
    }

    //异步调用
    @Test
    public void test3() throws Exception {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create("http://www.zx1989.top/")).build();
        BodyHandler<String> handler = HttpResponse.BodyHandlers.ofString();
        CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, handler);
        HttpResponse<String> result = response.get();
        String body = result.body();
        System.out.println(body);
    }

    @Test
    public void test4() {
        Set<String> set = Set.of("1", "2", "3");
        System.out.println(set);

        List<String> list = List.of("1", "2", "3");
        System.out.println(list);

        Map<String, String> map = Map.of("k1", "v1", "k2", "v2");
        System.out.println(map);
    }

    @Test
    public void test5() throws Exception {
        Date date = DateUtils.parseIso8601Date("2021-09-01");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        for (int i = 1; i <= 30; i++) {
            String yyyyMMdd = sdf.format(date);
            String url =
                    "http://bi.cs.sogou/bi/projectsqlmodeldatasapi?page=1&size=20&reportId=7613&modelId=13599&date="
                            + yyyyMMdd +
                            "&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoid2FuZ3NhaXBlbmciLCJ0b2tlbiI6IlpPRmtQaW0wYm5zcUVIeEUiLCJpYXQiOjE2Mzc2NjQxNDh9.5yCxYP3IUcMV3W32mcQjTCyajpbYoo-Bw7j2VzzEiLA";
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder(URI.create(url))
                    .build();
            BodyHandler<String> handler = HttpResponse.BodyHandlers.ofString();
            HttpResponse<String> response = client.send(request, handler);
            String body = response.body();

            JSONObject jsonObject = JSONObject.parseObject(body);

            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray list = data.getJSONArray("list");
            JSONObject obj = list.getJSONObject(0);
            BigDecimal qv1 = obj.getBigDecimal("qv1");
            BigDecimal qv2 = obj.getBigDecimal("qv2");
            BigDecimal qv3 = obj.getBigDecimal("qv3");
            System.out.println(yyyyMMdd + "\t" + qv1 + "\t" + qv2 + "\t" + qv3);
            date = DateUtil.offset(date, DateField.DAY_OF_MONTH, 1);
        }
    }

    @Test
    public void test6() throws Exception {
        Date date = DateUtils.parseIso8601Date("2021-08-31");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 1; i <= 95; i++) {
            String yyyyMMdd = sdf.format(date);
            String url = "https://roc.sogou-inc.com/project/tco/api/metrics/qv/qvdaycalculation/task?time=" + yyyyMMdd;
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
            BodyHandler<String> handler = HttpResponse.BodyHandlers.ofString();
            HttpResponse<String> response = client.send(request, handler);
            String body = response.body();
            System.out.println(yyyyMMdd + "~" + body);
            date = DateUtil.offset(date, DateField.DAY_OF_MONTH, 1);
        }
    }

    @Test
    public void test7() throws Exception {
        Date date = DateUtils.parseIso8601Date("2021-11-01");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 33; i++) {
            String yyyyMMdd = sdf.format(date);
            String url = "http://roc.sogou/project/tco/api/bill/summary/create/day?date=" + yyyyMMdd;
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
            BodyHandler<String> handler = HttpResponse.BodyHandlers.ofString();
            HttpResponse<String> response = client.send(request, handler);
            String body = response.body();
            System.out.println(yyyyMMdd + "~" + body);
            date = DateUtil.offset(date, DateField.DAY_OF_MONTH, 1);
        }
    }


}
